﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFChat.Model;
using WPFChat.View;
using WPFChat.ViewModel;
using be.metanous.mvvm.core;

namespace WPFChat.Controller
{
    public class ApplicationController
    {
        public MainWindow MainWindow { get; private set; }
        public ApplicationViewModel ApplicationViewModel { get; private set; }
        public NetworkController NetworkController { get; private set; }
        private Dictionary<string, Person> _persons;

        public ApplicationController()
        {
            ApplicationViewModel = new ApplicationViewModel();
            NetworkController = new NetworkController();

            _persons = new Dictionary<string, Person>();

            ApplicationViewModel.States.Add(new StateViewModel()
            {
                ContainedObject = State.Online,
                Description = "Online"
            });

            ApplicationViewModel.States.Add(new StateViewModel()
            {
                ContainedObject = State.Busy,
                Description = "Busy"
            });

            ApplicationViewModel.States.Add(new StateViewModel()
            {
                ContainedObject = State.Offline,
                Description = "Offline"
            });

            ApplicationViewModel.CurrentUser.State = ApplicationViewModel.States.First();
            ApplicationViewModel.CurrentUser.ContainedObject = new Person() {GUID = Guid.NewGuid().ToString()};

            ApplicationViewModel.ChatViewModel.SendCommand = SendCommandViewModel;

            MainWindow = new MainWindow()
            {
                DataContext = ApplicationViewModel
            };
        }

        public void Activate()
        {
            MainWindow.Show();
        }

        private CommandViewModel _sendCommandViewModel;

        public CommandViewModel SendCommandViewModel
        {
            get
            {
                if (_sendCommandViewModel == null)
                {
                    _sendCommandViewModel = new CommandViewModel(
                        new Command(SendMessage, CanSendMessage)) { Description = "Send" };
                }
                return _sendCommandViewModel;
            }
        }

        private bool CanSendMessage(object obj)
        {
            if (string.IsNullOrWhiteSpace(ApplicationViewModel.ChatViewModel.NewMessage))
            {
                return false;
            }

            if (string.IsNullOrWhiteSpace(ApplicationViewModel.CurrentUser.Name))
            {
                return false;
            }

            if ((State) ApplicationViewModel.CurrentUser.State.ContainedObject == State.Offline)
            {
                return false;
            }

            return true;
        }

        private void SendMessage(object obj)
        {
            var newMessage = new Message()
            {
                Sender = ApplicationViewModel.CurrentUser.Name,
                TimeStamp = DateTime.Now,
                Content = ApplicationViewModel.ChatViewModel.NewMessage,
                State = (State) ApplicationViewModel.CurrentUser.State.ContainedObject,
                GUID = ((Person) ApplicationViewModel.CurrentUser.State.ContainedObject).GUID
            };

            ApplicationViewModel.ChatViewModel.NewMessage = "";
            NetworkController.SendMessage(newMessage);
        }
    }
}
